package org.makcro.sms.services.chat.web.client.mvc.view;

import java.util.List;

import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.client.EventParams;
import org.makcro.sms.services.chat.web.client.widgets.AnswerDialog;
import org.makcro.sms.services.chat.web.shared.dto.HistoryItemDto;
import org.makcro.sms.services.chat.web.shared.dto.QuestionDto;

import com.extjs.gxt.ui.client.core.Template;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Params;

/**
 * View for controlling answer dialog
 * 
 * @author makcro
 * 
 */
public class AnswerView extends View {

	/**
	 * Dialog beeing controlled by this view
	 */
	private AnswerDialog	answerDialog;
	/**
	 * Template used to show answer statistics data
	 */
	private Template		answerStatsTemplate;

	public AnswerView(Controller controller) {
		super( controller );
	}

	@Override
	protected void initialize() {
		super.initialize();
		

	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType type = event.getType();

		if (type == AppEvents.PREPARE_ANSWER) {
			onPrepareAnswer( event );
		}

		else if (type == AppEvents.ANSWER_TEXT_CHANGED) {
			onAnswerTextChanged( event );
		}
		else if (type == AppEvents.ANSWER_SENT) {
			onAnswerSent( event );
		}
		else if (type == AppEvents.ANSWER_ABOUT_TO_BE_SENT) {
			onAnswerAboutToBeSent( event );
		}
		else if (type == AppEvents.ANSWER_SENDING_FAILED) {
			onAnswerSendingFailed( event );
		}
	}

	/**
	 * Re enables button bar and hids dialog
	 * @param event
	 */
	private void onAnswerSendingFailed(AppEvent event) {
		answerDialog.getButtonBar().enable();
		answerDialog.hide();
	}

	/**
	 * Disables buttons
	 * @param event
	 */
	private void onAnswerAboutToBeSent(AppEvent event) {
		answerDialog.getButtonBar().disable();
	}

	/**
	 * Enables buttons and hides
	 */
	private void onAnswerSent(AppEvent event) {
		answerDialog.getButtonBar().enable();
		answerDialog.hide();
	}

	public Template getAnswerStatsTemplate() {
		if (answerStatsTemplate == null) {
			answerStatsTemplate = new Template( "Broj karaktera u odgovoru: {charactetCount}" );
		}
		return answerStatsTemplate;
	}

	/**
	 * Updates answer statistics like character count. Invoked on answer text
	 * changed event
	 * 
	 * @param event
	 */
	private void onAnswerTextChanged(AppEvent event) {
		// get typed anwer text
		String answerText = answerDialog.getAnswerTextPanel().getTxtAnswerText().getValue();
		Integer answerTextLength = answerText.length();

		// Fill out template
		Params templateParams = new Params( "charactetCount", answerTextLength );
		String appledTemplate = getAnswerStatsTemplate().applyTemplate( templateParams );
		// set the value of the character counter
		answerDialog.getAnswerTextPanel().getLblAnswerStats().setText( appledTemplate );
	}

	/**
	 * Prepares answer dialog by filling it with history data. Invoked on
	 * {@link AppEvents#PREPARE_ANSWER} event
	 * 
	 * @param event used to carry hsitory data
	 */
	private void onPrepareAnswer(AppEvent event) {
		answerDialog = new AnswerDialog();
		// extract history data from event
		List<HistoryItemDto> historyItems = event.<List<HistoryItemDto>> getData();
		QuestionDto questionDto = event.<QuestionDto>getData(EventParams.QUESTION_DTO);
		
		ListStore<HistoryItemDto> store = answerDialog.getStore();
		// add items to store
		store.add( historyItems );
		answerDialog.getQuestionTextArea().setValue( questionDto.getQuestion() );
		// set selected item to the last, so it's visible automatically
		
		answerDialog.show();
	}

}
